
// 车险表格数据仓库
import router from '@/router/index'

import { creatEnum, deepCopy } from '@/utils/tools'

import {
    
    changeTabelToolsModel, 
    carTableSearchBarSwitch,
    updateCarTable,
    carCreatOrder,
    
} from '@/components/table_tools/index'

import carTableTitle from '@/components/ivew_conf/car_table_title'
import carTableDefaultReqParams from '@/components/ivew_conf/car_table_default_req_params'

class stateInf {

    toolsModel:tableInf.toolsMod
    toolsModels:any
    toolsContainerComponent:string
    toolsFunConf:commonInf.toolsBarItem[]
    tableReqParams:carOrderReq
    tableOrders:carTableOrder[]
    tabeTitleConf:tableInf.tableTitle[]
    selectedRow?:carTableOrder | null
    foldingLayer:tableInf.flodKey
    autoOpenFoldingLayer:boolean
    loading: boolean
    
}


class carTableStore {

    namespaced: true
    
    state:stateInf = {

        // 是否显示加载
        loading: true,

        // 工具条当前模式
        toolsModel: 'open', 

        // 工具条模式可切换关键字
        toolsModels: creatEnum(['open', 'close']),
        
        // 工具条操作容器当前加载组件
        toolsContainerComponent: 'c-table-search-bar',

        // 工具条操作面板模块配置
        toolsFunConf: [
            
            changeTabelToolsModel,
            carTableSearchBarSwitch,
            updateCarTable,
            carCreatOrder,

        ],

        // 表格数据列表请求参数
        tableReqParams: new carTableDefaultReqParams(), 

        // 表格数据列表
        tableOrders: [],

        // 表格表头配置
        tabeTitleConf: carTableTitle,
        
        // 被选中行数据
        selectedRow: null,

        // 是否显示折叠层 
        foldingLayer: 'close',

        // 点击表格项，自动展开折叠层
        autoOpenFoldingLayer: true,
        
    }

    mutations = {

        // 设置表格工具条加载
        setToolsFunConf(state, data){
            state['toolsFunConf'] = data
        },
        
        // 设置是否显示加载
        setLoading (state, type:boolean){
            state['loading'] = type;
        },
        
        // 切换折叠层
        foldingLayerSwitch(state:stateInf, data?:tableInf.flodKey){
            let newStatus:tableInf.flodKey = state.foldingLayer === 'open' ? 'close' : 'open';
            state.foldingLayer = data || newStatus;
            
            // 清除url缓存参数(防止刷新后， 再次进入表单界面)
            if(state.foldingLayer === 'close'){
                router.replace({query:{}})
            }

        },
        

        // 更新车险表格数据查询参数
        updateTableReqParams(state:stateInf, data:carOrderReq){
            state['tableReqParams'] = data;
        },


        // 设置表格选中项数据
        setSelectedRow(state:stateInf, row:carTableOrder){
            state.selectedRow = row;
        },
        

        // 缓存表格列表数据
        updateTableOrders(state, orders){
            state.tableOrders = orders || [];
        },
        
        // 设置工具条控制面板加载组件
        setToolsContainerComponent(state:stateInf, componentName:string){
            state.toolsContainerComponent = componentName;
        },

        // 工具条模式切换
        setToolsModel (state, type?:string){
            const mod:string = state['toolsModel'];
            let Models = state.toolsModels;
            let num:number = Models[mod];
            num+=1;
            
            state['toolsModel'] = type ||  Models[num] || Models[0];
        }
    }

    getters = {
        
        // 折叠层状态(是否显示)
        foldingLayerState (state:stateInf):boolean{
            return state.foldingLayer === 'open';
        },

    }

    actions = {

        /*
        ** 表格选中项操作分配
        ** @params:
            type: 操作类型 , update 更新选中项, clear 清除相中项
            data: 选中项数据
        */
        tableSelect ({ state, dispatch, commit }, {type, data}){

            const map = {
                clear: 'clearSelectedRow',
                update: 'updateSelectedRow'
            };

            dispatch(map[type], data)
        },  

        // 提交选中项数据
        updateSelectedRow(context, row:carTableOrder){

            // 刷新数据
            context.commit('setSelectedRow', row)

            if(context.state.autoOpenFoldingLayer){

                // 请求订单初始数据
                context.dispatch('carFormStore/initStore', {
                    orderId:row.orderId,
                    orderStatus:row.status
                }, {root:true}).then(success =>{
                    context.commit('foldingLayerSwitch', 'open')
                })
                
            }
        },

        // 清空选中仓库
        clearSelectedRow(context){
            context.commit('setSelectedRow', null)
        },
        
        // 初始设置
        initSet({state, commit, dispatch}){

            let tableReqParams = new carTableDefaultReqParams()
            let {company, department} = this['state']['mainStore']['userBase']
            tableReqParams =  Object.assign(deepCopy(tableReqParams), {
                memberCompany:company,
                memberDepartment:department
            })
                
            // 设置初始会员所属部门
            commit('updateTableReqParams', tableReqParams)
            
            // 刷新订单列表
            return dispatch('request', {
                url: 'get/orders',
                data: {}
            },{root:true}).then(success =>{
    
                // 清除加载提示
                commit('setLoading', false)
                if(success){
                    dispatch('recoveryForm')
                }
                
                return success
            })

        },

          // 恢复表单显示状态 (刷新后重加载)
          recoveryForm({state, commit, dispatch}){

            let query = router['history']['current']['query'];

            for(let item of  state.tableOrders){
                if(item.orderId === query.orderId){

                    // 高亮显示当前选中项
                    item['_highlight'] = true;
                    
                    // 模拟选中事件
                    dispatch('tableSelect',{
                        type: 'update',
                        data: item
                    })

                    return 
                }
            }

        }  
        
    }


}



export default carTableStore; 
